package com.haitonghop.modules.truyentranh;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.haitonghop.R;
import com.haitonghop.utils.Logger;

@SuppressLint("SetJavaScriptEnabled")
public class TruyenTranhCommentActivity extends Activity{

	WebView webView;
	LinearLayout loadingProcess;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();
		String value = "";
		if (extras != null) {
		    value = extras.getString("href");
		    Logger.logMe("value = "+value);
		}

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.haivl_activity_comment_layout);

		webView = (WebView)findViewById(R.id.webViewComment);
		loadingProcess = (LinearLayout)findViewById(R.id.loadingProcessComment);
		loadingProcess.setVisibility(View.VISIBLE);


		String url = "https://www.facebook.com/plugins/comments.php?api_key=354042864672977&locale=en_US&sdk=joey&" +
				"channel_url=http://static.ak.facebook.com/connect/xd_arbiter.php?version=18&cb=f31fac73a4&" +
				"origin"+value+"&domain=haibl.net&relation=parent.parent&numposts=10&width=655&" +
				"href="+value;
		Log.i("url", url);

		if(webView != null){
			webView.setVisibility(View.INVISIBLE);
//			webView.loadUrl(url);
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new FileReader("file:///android_asset/facebook.html"));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String line;
			String html1 = "";
			try {
			while((line = reader.readLine()) != null) {
				html1 += line;
			}

				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			webView.loadDataWithBaseURL(url, html1, "text/html", null, null);
			webView.setPadding(0, 0, 0, 0);
			webView.setInitialScale(100);
			webView.getSettings().setJavaScriptEnabled(true);
			webView.setWebViewClient(chromeClient);
		}

//		TextView textView = (TextView) findViewById(R.id.titleActivity);
//		textView.setText("Bình luận");
//
//		Button moreFunction = (Button) findViewById(R.id.btnMoreFunctions);
//		moreFunction.setVisibility(View.GONE);
//		Button back = (Button) findViewById(R.id.btnBackAcvitity);
//		if (back != null) {
//			back.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					finish();
//				}
//			});
//		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}


	WebViewClient chromeClient = new WebViewClient(){

		@Override
		public void onPageFinished(WebView view, String url) {
			loadingProcess.setVisibility(View.GONE);
			webView.setVisibility(View.VISIBLE);
		};

		@Override
		public void onFormResubmission(WebView view, Message dontResend, Message resend) {
			Logger.logMe("dontResend = "+dontResend.obj);
			Logger.logMe("dontResend = "+dontResend.arg1);
			Logger.logMe("dontResend = "+dontResend.arg2);
			Logger.logMe("dontResend = "+dontResend.what);

			Logger.logMe("resend = "+resend.obj);
			Logger.logMe("resend = "+resend.arg1);
			Logger.logMe("resend = "+resend.arg2);
			Logger.logMe("resend = "+resend.what);
			// TODO Auto-generated method stub
			super.onFormResubmission(view, dontResend, resend);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Logger.logMe("url = "+url);
			return false;
		};
	};
}
